#include "head.h"
// 客户读取
u_list *Log_In(u_list *list)
{

    if (list == NULL) // 判断头文件创建成功没有
    {
        ERRLOG("创建失败");
        return NULL;
    }
    FILE *fp0 = fopen(REGULAR_FILE, "r"); // 打开文件读取用户
    FILE *fp1 = fopen(SILVER_FILE, "r");
    FILE *fp2 = fopen(GOLD_FILE, "r");
    FILE *fp3 = fopen(PLATINUM_FILE, "r");
    if (fp0 == NULL) // 判断四个文件是非全部打开
    {
        ERRLOG("文件fp0打开失败");
        return NULL;
    }
    if (fp1 == NULL)
    {
        ERRLOG("文件fp1打开失败");
        return NULL;
    }
    if (fp2 == NULL)
    {
        ERRLOG("文件fp2打开失败");
        return NULL;
    }
    if (fp3 == NULL)
    {
        ERRLOG("文件fp3打开失败");
        return NULL;
    }
    char ch;
    int len[4] = {0};                // 创建四个数字存放四个文件的用户人数
    while ((ch = fgetc(fp0)) != EOF) // 判断文件人数
    {
        if (ch == '\n')
        {
            len[0]++;
        }
    }
    while ((ch = fgetc(fp1)) != EOF)
    {
        if (ch == '\n')
        {
            len[1]++;
        }
    }
    while ((ch = fgetc(fp2)) != EOF)
    {
        if (ch == '\n')
        {
            len[2]++;
        }
    }
    while ((ch = fgetc(fp3)) != EOF)
    {
        if (ch == '\n')
        {
            len[3]++;
        }
    }
    user_t data;
    int len_num = 0;
    int i;
    fseek(fp0, 0, SEEK_SET); // 获取文件用户个数后，把光标位移行首
    fseek(fp1, 0, SEEK_SET);
    fseek(fp2, 0, SEEK_SET);
    fseek(fp3, 0, SEEK_SET);
    for (int j = 0; j < len[0]; j++) // 写入文件
    {
        fscanf(fp0, "%s\t%s\t%d\t%d\t%d\t%d\n", data.name, data.passwd, &data.IDcard, &data.value, &data.card, &data.ticket);
        for ( i = 0; i < data.ticket; i++)
        { // 写入文件
            fscanf(fp0, "%d\t", &data.order[i].id);
            fscanf(fp0, "%s\t", data.order[i].flighttype);
            fscanf(fp0, "%s\t", data.order[i].startcity);
            fscanf(fp0, "%s\t", data.order[i].arrivecity);
            fscanf(fp0, "%d:%d\t", &data.order[i].starttime[0], &data.order[i].starttime[1]);
            fscanf(fp0, "%d:%d\t", &data.order[i].arrivetime[0], &data.order[i].arrivetime[1]);
            fscanf(fp0, "%s\t", data.order[i].flytime);
            fscanf(fp0, "%d\t", &data.order[i].value);
            fscanf(fp0, "%d\t", &data.order[i].maxNUM);
            fscanf(fp0, "%d\t", &data.order[i].leftbuyersa);
            fscanf(fp0, "%c\n", &data.order[i].whetherfly);
            j++;
        }
        insert_node(list, data, len_num);
        len_num++;
    }
    for (int j = 0; j < len[1]; j++)
    {
        fscanf(fp1, "%s\t%s\t%d\t%d\t%d\t%d\n", data.name, data.passwd, &data.IDcard, &data.value, &data.card, &data.ticket);
        for (size_t i = 0; i < data.ticket; i++)
        { // 写入文件
            fscanf(fp1, "%d\t", &data.order[i].id);
            fscanf(fp1, "%s\t", data.order[i].flighttype);
            fscanf(fp1, "%s\t", data.order[i].startcity);
            fscanf(fp1, "%s\t", data.order[i].arrivecity);
            fscanf(fp1, "%d:%d\t", &data.order[i].starttime[0], &data.order[i].starttime[1]);
            fscanf(fp1, "%d:%d\t", &data.order[i].arrivetime[0], &data.order[i].arrivetime[1]);
            fscanf(fp1, "%s\t", data.order[i].flytime);
            fscanf(fp1, "%d\t", &data.order[i].value);
            fscanf(fp1, "%d\t", &data.order[i].maxNUM);
            fscanf(fp1, "%d\t", &data.order[i].leftbuyersa);
            fscanf(fp1, "%c\n", &data.order[i].whetherfly);
            j++;
        }
        insert_node(list, data, len_num);
        len_num++;
    }
    for (int j = 0; j < len[2]; j++)
    {
        fscanf(fp2, "%s\t%s\t%d\t%d\t%d\t%d\n", data.name, data.passwd, &data.IDcard, &data.value, &data.card, &data.ticket);
        for ( i = 0; i < data.ticket; i++)
        { // 写入文件
            fscanf(fp2, "%d\t", &data.order[i].id);
            fscanf(fp2, "%s\t", data.order[i].flighttype);
            fscanf(fp2, "%s\t", data.order[i].startcity);
            fscanf(fp2, "%s\t", data.order[i].arrivecity);
            fscanf(fp2, "%d:%d\t", &data.order[i].starttime[0], &data.order[i].starttime[1]);
            fscanf(fp2, "%d:%d\t", &data.order[i].arrivetime[0], &data.order[i].arrivetime[1]);
            fscanf(fp2, "%s\t", data.order[i].flytime);
            fscanf(fp2, "%d\t", &data.order[i].value);
            fscanf(fp2, "%d\t", &data.order[i].maxNUM);
            fscanf(fp2, "%d\t", &data.order[i].leftbuyersa);
            fscanf(fp2, "%c\n", &data.order[i].whetherfly);
            j++;
        }
        insert_node(list, data, len_num);
        len_num++;
    }

    for (int j = 0; j < len[3]; j++)
    {
        fscanf(fp3, "%s\t%s\t%d\t%d\t%d\t%d\n", data.name, data.passwd, &data.IDcard, &data.value, &data.card, &data.ticket);
        for ( i = 0; i < data.ticket; i++)
        { // 写入文件
            fscanf(fp3, "%d\t", &data.order[i].id);
            fscanf(fp3, "%s\t", data.order[i].flighttype);
            fscanf(fp3, "%s\t", data.order[i].startcity);
            fscanf(fp3, "%s\t", data.order[i].arrivecity);
            fscanf(fp3, "%d:%d\t", &data.order[i].starttime[0], &data.order[i].starttime[1]);
            fscanf(fp3, "%d:%d\t", &data.order[i].arrivetime[0], &data.order[i].arrivetime[1]);
            fscanf(fp3, "%s\t", data.order[i].flytime);
            fscanf(fp3, "%d\t", &data.order[i].value);
            fscanf(fp3, "%d\t", &data.order[i].maxNUM);
            fscanf(fp3, "%d\t", &data.order[i].leftbuyersa);
            fscanf(fp3, "%c\n", &data.order[i].whetherfly);
            j++;
        }
        insert_node(list, data, len_num);
        len_num++;
    }
    return list; // 返回头文件
}
// 注册
u_list *register_u_node(u_list *list)
{
    printf("\033[1;31;40m请输入要注册的名字:\033[0m");
    char name[128];        // 存放注册姓名
    char password[2][128]; // 存放注册密码
    scanf("%s", name);
    int num = 0;
    do
    {
        if (num)
            printf("\033[1;31;40m密码不相等，请重新输入\033[0m\n");
        printf("\033[1;31;40m请输入密码:\033[0m");
        scanf("%s", password[0]);
        printf("\033[1;31;40m请再次输入密码:\033[0m"); // 防止判断密码输出错，需要输入两次
        scanf("%s", password[1]);
        num++;
    } while (strcmp(password[0], password[1])); // 判断两次密码是否相同
    user_t data;                      // 创建用户构体
    strcpy(data.name, name);          // 将名字复制到用户结构体里面
    strcpy(data.passwd, password[0]); // 蒋密码也复制到用户结构体里面
    data.ticket = 0;
    data.IDcard = 0; // 创建的用户默认给用户等级给普通用户，余额也是零
    data.value = 0;

    insert_node(list, data, 2); // 用户插入链表
    // print_u_list(head);
    insert_file_u_node(list); // 将用户写入文件
    printf("\033[1;34;40m注册成功\033[0m\n");
    return list;
}